/*
 *  @product.title@
 *  Copyright (C) @product.copyright-year@
 *    @product.vendor@
 *    Registration Number: @product.registration-number@
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package jp.go.aist.six.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;




/**
 * An utility class for ISO date/time format.
 *
 * @author  Akihito Nakamura, AIST
 * @version $Id: IsoDate.java 300 2011-03-02 05:45:46Z nakamura5akihito $
 */
public class IsoDate
{

    /**
     * The date format.
     */
    private static SimpleDateFormat  _DATE_FORMATTER_ =
        new SimpleDateFormat( "yyyy-MM-dd" );



    /**
     * The date/time format.
     */
    private static SimpleDateFormat  _DATETIME_FORMATTER_ =
        new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss" );


    /**
     * The date/time format, including milliseconds.
     */
    private static SimpleDateFormat  _DATETIME_MILLISEC_FORMATTER_ =
        new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS" );



    /**
     * The date/time format, including milliseconds and time zone.
     */
    private static SimpleDateFormat  _DATETIME_MILLISEC_TZ_FORMATTER_ =
        new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" );




    /**
     */
    public static String format(
                    final Date date
                    )
    {
        if (date == null) {
            return null;
        }

        return _DATETIME_FORMATTER_.format( date );
    }



    /**
     */
    public static String formatDate(
                    final Date date
                    )
    {
        if (date == null) {
            return null;
        }

        return _DATE_FORMATTER_.format( date );
    }



    /**
     */
    public static Date valueOf(
                    final String value
                    )
    {
        Date  date = null;
        if (value != null) {
            SimpleDateFormat  format = null;
            if (value.length() <= 10) {
                format = _DATE_FORMATTER_;
            } else if (value.length() <= 19) {
                format = _DATETIME_FORMATTER_;
            } else if (value.length() <= 23) {
                format = _DATETIME_MILLISEC_FORMATTER_;
            } else {
                format = _DATETIME_MILLISEC_TZ_FORMATTER_;
            }
            try {
                date = format.parse( value );
            } catch (ParseException ex) {
                throw new IllegalArgumentException( ex.getMessage() );
            }
        }

        return date;
    }



    /**
     */
    public static Date dateValueOf(
                    final String value
                    )
    {
        Date  date = null;
        if (value != null) {
            try {
                date = _DATE_FORMATTER_.parse( value );
            } catch (ParseException ex) {
                throw new IllegalArgumentException( ex.getMessage() );
            }
        }

        return date;
    }

}
// IsoDate

/* vim:set tabstop=4:set expandtab:set shiftwidth=4: */

